/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oyun.annotation.Log;
import com.oyun.modules.medicine.domain.AdministrativeOffice;
import com.oyun.modules.medicine.domain.vo.AdministrativeOfficeQueryCriteria;
import com.oyun.modules.medicine.service.AdministrativeOfficeService;
import com.oyun.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author aoyun
* @date 2024-04-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "科室管理管理")
@RequestMapping("/api/administrativeOffice")
public class AdministrativeOfficeController {

    private final AdministrativeOfficeService administrativeOfficeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('administrativeOffice:list')")
    public void exportAdministrativeOffice(HttpServletResponse response, AdministrativeOfficeQueryCriteria criteria) throws IOException {
        administrativeOfficeService.download(administrativeOfficeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询科室管理")
    @ApiOperation("查询科室管理")
    @PreAuthorize("@el.check('administrativeOffice:list')")
    public ResponseEntity<PageResult<AdministrativeOffice>> queryAdministrativeOffice(AdministrativeOfficeQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(administrativeOfficeService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增科室管理")
    @ApiOperation("新增科室管理")
    @PreAuthorize("@el.check('administrativeOffice:add')")
    public ResponseEntity<Object> createAdministrativeOffice(@Validated @RequestBody AdministrativeOffice resources){
        administrativeOfficeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改科室管理")
    @ApiOperation("修改科室管理")
    @PreAuthorize("@el.check('administrativeOffice:edit')")
    public ResponseEntity<Object> updateAdministrativeOffice(@Validated @RequestBody AdministrativeOffice resources){
        administrativeOfficeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除科室管理")
    @ApiOperation("删除科室管理")
    @PreAuthorize("@el.check('administrativeOffice:del')")
    public ResponseEntity<Object> deleteAdministrativeOffice(@RequestBody List<Long> ids) {
        administrativeOfficeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("获取科室名称")
    @GetMapping("/getNameAndId")
    public ResponseEntity<Object> getNameAndId(){
       return new ResponseEntity<>(administrativeOfficeService.getNameAndId(),HttpStatus.OK);
    }

    @GetMapping("getOfficeByhpId/{hpId}/{pageNum}/{pageSize}")
    @ApiOperation("根据医院id获取科室")
    public ResponseEntity<Object> getOfficeByHpId(@PathVariable("hpId" ) Long hpId,@PathVariable("pageNum") Long pageNum,@PathVariable("pageSize") Long pageSize){
       return new ResponseEntity<>(administrativeOfficeService.getOfficeByHpId(hpId,pageNum,pageSize),HttpStatus.OK);
    }

    @ApiOperation("根据名称查询")
    @GetMapping("/getByName/{name}")
    public ResponseEntity<Object> getByUsername(@PathVariable("name") String name) {
        return new ResponseEntity<>(administrativeOfficeService.getByUsername(name), HttpStatus.OK);
    }
}
